import * as managerModel from '../../models/manager';
import { ValidException } from '../../helps/exception';

/**
 * 登录
 *
 * @export
 * @param {any} { username, password }
 * @returns {any}
 */
export function* login({ username, password }) {
  if (!username || !password) throw ValidException('用户名或密码不能为空');
  const manager = yield managerModel.findUsername(username);
  if (!manager) throw ValidException('用户名或密码错误');
  if (!manager.validatePassword(password)) throw ValidException('用户名或密码错误');
  return manager;
}

export function* editPassword({ id, password }) {
  if (!password) throw ValidException('密码 不能为空');
  const b = yield managerModel.editPassword(id, password);
  if (!b) throw ValidException('密码修改失败');
  return true;
}

/**
 * 刷新
 *
 * @export
 * @returns
 */
export function* refresh() {
  return {};
}
